package com.cms.jy.draft.util;

public class AudioVolumeConverter {

    /**
     * 将dB值转换为线性值
     * @param dB dB值
     * @return 线性值
     */
    public static double dBToLinear(double dB) {
        return Math.pow(10, dB / 20.0);
    }

    /**
     * 将线性值转换为dB值
     * @param linear 线性值
     * @return dB值
     */
    public static double linearToDB(double linear) {
        return 20 * Math.log10(linear);
    }

    public static void main(String[] args) {
        // 验证转换 Double.NEGATIVE_INFINITY
        System.out.println("7.4dB -> " + dBToLinear(7.4)); // 约等于 2.3357
        System.out.println("-20.7dB -> " + dBToLinear(-20.7)); // 约等于 0.0922
        System.out.println("0.0922 -> " + linearToDB(0.09223851561546326) + "dB"); // 约等于 -20.7dB
        System.out.println("2.3357 -> " + linearToDB(2.335721492767334) + "dB"); // 约等于 7.4dB
    }
}

